package com.hc.appanimal;

import java.lang.reflect.Method;

import com.hc.DTO.AnimalDTO;
import com.hc.DTO.TokenDTO;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;

public class MapActivity extends Activity implements OnClickListener {
	WebView wvMap;
	TextView txtTitle;
	Button btnBack;
	AnimalDTO dto;
	Button btnMap, btnGallery, btnHome, btnDetail;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_map);
		txtTitle = (TextView) findViewById(R.id.txtTitle);
		txtTitle.setTextSize(15.0f);
		btnBack = (Button) findViewById(R.id.btnBack);
		btnBack.setVisibility(View.VISIBLE);
		btnBack.setOnClickListener(this);
//		wvTradeMapDesc = (WebView) findViewById(R.id.wvTradeMapDesc);
		Bundle data = getIntent().getExtras();
		if (data != null) {
			dto = (AnimalDTO) data.getSerializable("images");
		}

		String TradeMapCoords = dto.getTradeMapCoords();
		
		if (TradeMapCoords.equalsIgnoreCase("null"))
			TradeMapCoords = "";
		String CommonName = dto.getCommonName();
		if (CommonName.equalsIgnoreCase("null"))
			CommonName = "";
		
		txtTitle.setText(CommonName);
		wvMap = (WebView) findViewById(R.id.wvMap);
		WebSettings webViewSettings = wvMap.getSettings();
		webViewSettings.setJavaScriptCanOpenWindowsAutomatically(true);
		webViewSettings.setJavaScriptEnabled(true);
		// webViewSettings.setPluginsEnabled(true);
		webViewSettings.setBuiltInZoomControls(true);
		webViewSettings.setPluginState(PluginState.ON);
		wvMap.loadDataWithBaseURL(null,
				"<html><body background=\"#00000000\" link=\"white\" >"
						+ TradeMapCoords + "</body</html>", "text/html",
				"UTF-8", null);
		wvMap.setBackgroundColor(0x00000000);
		if (Build.VERSION.SDK_INT >= 11) // Android v3.0+
			try {
				Method method = View.class.getMethod("setLayerType", int.class,
						Paint.class);
				method.invoke(wvMap, 1, new Paint()); // 1 =
														// LAYER_TYPE_SOFTWARE
														// (API11)
			} catch (Exception e) {
			}

//		wvTradeMapDesc.loadDataWithBaseURL(null,
//				"<html><body background=\"#00000000\" link=\"white\"><font color=\"black\">"
//						+ "<p text-align=\"right\">" + "<b>Trade Details</b><br>"
//						+ tradeInfo + TradeMapDesc + "</p>"
//						+ "</font></body</html>", "text/html", "UTF-8", null);
//		wvTradeMapDesc.setBackgroundColor(0x00000000);
//		if (Build.VERSION.SDK_INT >= 11) // Android v3.0+
//			try {
//				Method method = View.class.getMethod("setLayerType", int.class,
//						Paint.class);
//				method.invoke(wvTradeMapDesc, 1, new Paint()); // 1 =
//																// LAYER_TYPE_SOFTWARE
//																// (API11)
//			} catch (Exception e) {
//			}
		btnMap = (Button) findViewById(R.id.btnMap);
		btnGallery = (Button) findViewById(R.id.btnGallery);
		btnMap.setOnClickListener(this);
		btnGallery.setOnClickListener(this);
		btnHome = (Button) findViewById(R.id.btnHome);
		btnHome.setOnClickListener(this);
		btnDetail = (Button) findViewById(R.id.btnDetail);
		btnDetail.setOnClickListener(this);
		btnMap.setBackgroundResource(R.drawable.animal_mapf);
		btnGallery.setBackgroundResource(R.drawable.animal_images);
		btnDetail.setBackgroundResource(R.drawable.animal_details);

	}

	@Override
	public void onClick(View v) {
		Intent intent;
		Bundle data;
		switch (v.getId()) {
		case R.id.btnBack:
			finish();
			break;
		case R.id.btnGallery:
			intent = new Intent(MapActivity.this, GalleryActivity.class);
			data = new Bundle();
			data.putSerializable("images", dto);
			intent.putExtras(data);
			startActivity(intent);
			finish();
			break;
		case R.id.btnHome:
			intent = new Intent(MapActivity.this, SummaryActivity.class);
			data = new Bundle();
			data.putSerializable("images", dto);
			intent.putExtras(data);
			startActivity(intent);
			finish();
			break;
		case R.id.btnDetail:
			intent = new Intent(MapActivity.this, DetailAnimalActivity.class);
			data = new Bundle();
			data.putSerializable("images", dto);
			intent.putExtras(data);
			startActivity(intent);
			finish();
			break;

		default:
			break;
		}

	}
}
